package org.cloudie.docs.editor.client.windows;

import org.cloudie.docs.editor.client.commands.generic.ExplorerWindowOpenVirtualFolderCommand;
import org.cloudie.docs.editor.client.commands.generic.VirtualEntryFolderRefreshCommand;
import org.cloudie.docs.editor.client.commands.generic.VirtualEntryMoveCommand;
import org.cloudie.docs.editor.client.events.CommandEvent;
import org.cloudie.docs.editor.client.icons.explorer.virtual.VirtualFolderExplorerIcon;
import org.cloudie.docs.editor.client.icons.explorer.virtual.VirtualShortcutExplorerIcon;
import org.cloudie.docs.editor.client.storage.items.FolderVirtualSystemEntry;
import org.cloudie.docs.editor.client.storage.items.VirtualSystemEntry;
import org.cloudie.docs.editor.client.widgets.DesktopIcon;
import org.cloudie.docs.editor.client.widgets.ExplorerIcon;

import com.google.gwt.user.client.ui.Widget;

public class DesktopExplorerWindow extends ExplorerWindow<VirtualSystemEntry> {

  public DesktopExplorerWindow() {
    super("Desktop Explorer", new String[] { "Name", "Type" }, false);
  }
  
  @Override
  public void show(VirtualSystemEntry folder, VirtualSystemEntry[] entries) {
	super.show(folder, entries);
	if (folder.getExplorerEntryId() != null &&
		folder.getExplorerEntryParentId() != null) {
		VirtualFolderExplorerIcon folderIcon =
		  new VirtualFolderExplorerIcon(folder.getExplorerEntryParentId(),
			  "..",
		      "filesystems/folder",
		      "Open parent folder",
		      new ExplorerWindowOpenVirtualFolderCommand(folder.getExplorerEntryParentId()));
		folderIcon.setMovable(false);
		addEntry(folderIcon, new String[] { "..", "" });
	}
    for(VirtualSystemEntry entry : entries) {
      addEntry(entry);
    }
    show();
  }
  
  protected void addEntry(VirtualSystemEntry entry) {
    if (entry instanceof FolderVirtualSystemEntry) {
      VirtualFolderExplorerIcon folderIcon = new VirtualFolderExplorerIcon(
          entry.getExplorerEntryId(),
          entry.getExplorerEntryLabel(),
          entry.getImage(),
          "Open " + entry.getExplorerEntryLabel(),
          new ExplorerWindowOpenVirtualFolderCommand(entry.getExplorerEntryId()));
	  addEntry(folderIcon, new String[] { entry.getExplorerEntryLabel(), "Folder" });
    } else {
      VirtualShortcutExplorerIcon fileIcon = new VirtualShortcutExplorerIcon(entry.getExplorerEntryId(),
          entry.getExplorerEntryLabel(), entry.getImage(), "Open " + entry.getExplorerEntryLabel(),
          entry.getAction());
	  addEntry(fileIcon, new String[] { entry.getExplorerEntryLabel(), "Shortcut" });
    }
  }

  @Override
  public void refresh() {
	clearEntries();
    CommandEvent.fire(new VirtualEntryFolderRefreshCommand(currentFolder.getExplorerEntryId()));
  }
  
  @Override
  public boolean acceptsReceives(Widget dropped, boolean isFinal) {
	if (dropped instanceof DesktopIcon) {
	  DesktopIcon di = (DesktopIcon) dropped;
	  if (di.getId().equals(currentFolder.getExplorerEntryId())) {
	    return false;
	  }
	  return true;
	} else if (dropped instanceof ExplorerIcon) {
	  return true;
	}
    return true;
  }
  
  @Override
  public boolean acceptsReceives() {
    return true;
  }
  
  @Override
  public void onReceive(Widget dropped, int x, int y) {
	if (dropped instanceof DesktopIcon) {
      DesktopIcon di = (DesktopIcon) dropped;
      VirtualEntryMoveCommand cmd =
          new VirtualEntryMoveCommand(di.getId(),
        	currentFolder.getExplorerEntryId(), x, y, -1);
      CommandEvent.fire(cmd);
	}
    refresh();
  }

}
